from django.contrib.auth.mixins import LoginRequiredMixin
from django import http

from meiduo_mall.utils.response_code import RETCODE


class LoginRequiredJSONMixin(LoginRequiredMixin):
    """自定义判断用户是否登录的扩展类：返回JSON"""

    # 为什么只需要重写handle_no_permission(self)方法？
    # 因为判断用户是否登录的操作，父类已经完成，
    # 子类只需要关心如果用户未登录对应怎样的操作即可
    def handle_no_permission(self):
        """直接响应JSON数据"""
        return http.JsonResponse({"code":RETCODE.SESSIONERR,"errmsg":"用户未登录"})

"""
class LoginRequiredMixin(AccessMixin):
    def dispatch(self, request, *args, **kwargs):
        if not request.user.is_authenticated:
            return self.handle_no_permission()
        return super().dispatch(request, *args, **kwargs)
 
def handle_no_permission(self):
    if self.raise_exception or self.request.user.is_authenticated:
        raise PermissionDenied(self.get_permission_denied_message())
    return redirect_to_login(
        path,
        resolved_login_url,
        self.get_redirect_field_name(),
    ) 
源码分析      
"""